//
//  BaseToolViewController.m
//  JiFengDaRen
//
//  Created by zhuchao on 16/3/21.
//  Copyright © 2016年 SH_iOS. All rights reserved.
//

#import <WebKit/WebKit.h>
#import <JavaScriptCore/JavaScriptCore.h>
#import "BaseToolViewController.h"

@interface BaseToolViewController ()<UIWebViewDelegate>
{
    UIWebView *_webView;
}
@property (nonatomic, strong) JSContext *jsContext;



@end

@implementation BaseToolViewController

- (BOOL)prefersStatusBarHidden{
    
    return self.isHiddenNavagationBar;
}
- (void)viewWillAppear:(BOOL)animated{
    
    if (self.isHiddenNavagationBar) {
        self.navigationController.navigationBar.hidden = YES;
        
    }else{
        self.navigationController.navigationBar.hidden = NO;
        
    }
    self.tabBarController.tabBar.hidden = YES;
    [_webView reload];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self configUI];
    
    
}
#pragma mark -- 初始化UI
- (void)configUI{
    
    self.view.backgroundColor = [UIColor whiteColor];
    _webView = [[UIWebView alloc]initWithFrame:CGRectMake(0,0,KScreenWidth, KScreenHeight - KNAVIGATIONBARHEIGHT )];
    
    
    //设置代理
    _webView.delegate = self;
    _webView.opaque = YES;
    
    //缩放整个scrollview
    _webView.scalesPageToFit = YES;
    
    
    //加载网页
    [_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.urlString]]];
    
    [self.view addSubview:_webView];
}

#pragma mark -- 返回
- (void)back:(UIBarButtonItem *)item{
    [self goToCack];
}

#pragma mark - WebViewDelegate
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
    SHLog(@"加载失败%@",error.localizedDescription);
    
}

- (void)webViewDidStartLoad:(UIWebView *)webView{
    
    NSString *sessionid = [Tool objectForKey:@"sessionid"];
    
    if (!sessionid) {
        return;
    }
    self.jsContext = [webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
    self.jsContext[@"getSessionId"] = ^(){
        //从沙盒中获取sesionid
        
        
        return [@"sessionid=" stringByAppendingString:sessionid];
    };
    
    __weak typeof(self) weakSelf = self;
    self.jsContext[@"showSessionid"] = ^(NSString *sessionid){
        
        UIAlertView *al = [[UIAlertView alloc]initWithTitle:@"显示" message:sessionid delegate:weakSelf cancelButtonTitle:@"是" otherButtonTitles:@"否", nil];
        [al show];
    };
    
    self.jsContext.exceptionHandler = ^(JSContext *context, JSValue *exceptionValue) {
        context.exception = exceptionValue;
        SHLog(@"异常信息：%@", exceptionValue);
    };
    
}


//网页加载完成
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    
    
    [MBProgressHUD hideHUDForView:self.view animated:YES];
    NSString *sessionid = [Tool objectForKey:@"sessionid"];
    
    if (!sessionid) {
        return;
    }
    self.jsContext = [webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
    self.jsContext[@"getSessionId"] = ^(){
        //从沙盒中获取sesionid
        
        
        return [@"sessionid=" stringByAppendingString:sessionid];
    };
    __weak typeof(self) weakSelf = self;
    
    
    //定义好JS要调用的方法, share就是调用的share方法名
    //cloudStoreShare(name,url,imgurl);
    self.jsContext[@"cloudStoreShare"] =  ^(NSString *name ,NSString * url,NSString *imgUrl) {
        
        
        
        dispatch_async(dispatch_get_main_queue(), ^{
            
            NSArray *args = [JSContext currentArguments];
            
            for (JSValue *jsVal in args) {
                SHLog(@"%@", jsVal.toString);
            }
            
            
            
        });
        
    };
    //头像
    self.jsContext[@"setImage"] = ^(int pic_type){
        
    };
    /**
     *  返回
     */
    self.jsContext[@"goBack"] = ^{
        
        
        dispatch_async(dispatch_get_main_queue(), ^{
            
            [weakSelf goToCack];
            
        });
        
    };
    
    
    self.jsContext[@"goOfficials"] = ^{
        
        
        
    };
    
    
}

#pragma mark -- 返回
- (void)goToCack{
    
    [[NSURLCache sharedURLCache] removeAllCachedResponses];
    if (self.isHiddenGoBack) {
        [self dismissViewControllerAnimated:YES completion:nil];
        
    }else{
        if (_webView.canGoBack) {
            
            [_webView goBack];
            
        }else{
            [self.navigationController popViewControllerAnimated:YES];
            
        }
        
    }
}








- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
